package appnode;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;
import javax.servlet.*;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import appnode.Node;
import appnode.Linktools;
import appnode.SearchNode;
import appnode.PMF;

public class Addlink extends HttpServlet {
	
	 public void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
			
			PersistenceManager pm = PMF.get().getPersistenceManager();	
			String action = req.getParameter("action");
			String nodename1 = req.getParameter("nodename1");
			String nodename2 = req.getParameter("nodename2");
			int i = 0;
			req.setAttribute("nodenam1",nodename1);
			req.setAttribute("nodenam2",nodename2);
			
			// check
			if (action.equals("check")) {
				
				
				Key keynode1 = KeyFactory.stringToKey(nodename1);
				Node node1 = pm.getObjectById(Node.class,keynode1);
				//Key keynode2 = KeyFactory.stringToKey(nodename2);
				Node node2 = SearchNode.getByName(nodename2);
				
								
				// check if there is a link between nodes
				if (Linktools.validlink(node1,node2).equals("yes")) {
					resp.sendRedirect("/addlink.jsp?nodename1="+nodename1+"&nodename2="+nodename2);
				}
				else if (Linktools.validlink(node1,node2).equals("no")) {
					resp.setContentType("text/html");
					resp.getWriter().print("<html>");
					resp.getWriter().print("<head><title>Add a link</title></head>");
					resp.getWriter().print("You have already a link with this node");
					resp.getWriter().print("<br>");
					resp.getWriter().print("<input type='button' value='Ok' onClick='self.close()'/>");
					resp.getWriter().print("</html>");
				}
			
			} // check
			
			// add
			if (action.equals("add")) {
				
				String keystrif1 = req.getParameter("keystrif1");
				String keystrif2 = req.getParameter("keystrif2");
				String protocol = req.getParameter("protocol");
				String freq = req.getParameter("freq");
				String power = req.getParameter("power");
				
				Key keyif1 = KeyFactory.stringToKey(keystrif1);
				Interface if1 = pm.getObjectById(Interface.class,keyif1);
				
				// add frominterface
				if1.setFrominterface(keystrif2);
				
				// check if link is estblished
				Key keyif2 = KeyFactory.stringToKey(keystrif2);
				Interface if2 = pm.getObjectById(Interface.class,keyif2);
				
				if (if2.getFrominterface()!=null) {
					if (if2.getFrominterface().equals(keystrif1)) {
						// new link
						Link link = new Link(keystrif1,keystrif2,protocol,freq,power, new Date());
						pm.makePersistent(link);
						if1.setLink(link.getKeyStr());
						if2.setLink(link.getKeyStr());
						resp.setContentType("text/html");
						resp.getWriter().print("<head><title>Add a link</title></head>");
						//resp.getWriter().print("<link rel='stylesheet' href='map.css' type='text/css' media='screen' title='mapcss'></link>");
						resp.getWriter().print("<html>");
						resp.getWriter().print("<div style='font-family:Verdana;font-size: smaller;'>New link!</div>");
						resp.getWriter().print("<br>");
						resp.getWriter().print("<input type='button' value='Ok' onClick='window.close();window.opener.location.reload();'/>");
						resp.getWriter().print("</html>");
					} // if		
					else {
						// no links
						resp.setContentType("text/html");
						resp.getWriter().print("<head><title>Add a link</title></head>");
						//resp.getWriter().print("<link rel='stylesheet' href='map.css' type='text/css' media='screen' title='mapcss'></link>");
						resp.getWriter().print("<html>");
						resp.getWriter().print("<div style='font-family:Verdana;font-size: smaller;'>You have to wait for" + if2.getNode().getNodename()+"'s responce</div>");
						resp.getWriter().print("<br>");
						resp.getWriter().print("<input type='button' value='Ok' onClick='self.close()'/>");
						resp.getWriter().print("</html>");
					}
				
				}
				else {
					resp.setContentType("text/html");
					resp.getWriter().print("<head><title>Add a link</title></head>");
					//resp.getWriter().print("<link rel='stylesheet' href='map.css' type='text/css' media='screen' title='mapcss'></link>");
					resp.getWriter().print("<html>");
					resp.getWriter().print("<div style='font-family:Verdana;font-size: smaller;'>You have to wait for" + if2.getNode().getNodename()+"'s responce</div>");
					resp.getWriter().print("<br>");
					resp.getWriter().print("<input type='button' value='Ok' onClick='self.close()'/>");
					resp.getWriter().print("</html>");	
					
				} // if
			pm.close();	
			} // add
		}	
}